package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ttg.model.mapper.QualityManageLadderRuleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.model.po.QualityManageLadderRulePO;
import com.ttg.model.pojo.QualityManageLadderRule;
import com.ttg.model.pojo.QualityManageWeightConfig;
import com.ttg.web.dao.QualityManageLadderRuleDao;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 质量管理-阶梯评价规则表(TblQualityManageLadderRule) daoImpl
 *
 * @author chenyou
 * @datetime 2025-08-18 14:50:36
 */
@Service
public class QualityManageLadderRuleDaoImpl extends ServiceImpl<QualityManageLadderRuleMapper, QualityManageLadderRule> implements QualityManageLadderRuleDao {

    @Override
    public QualityManageLadderRule getByUnique(Integer dimType, Integer type, Integer orderId, Long orgNo) {
        LambdaQueryWrapper<QualityManageLadderRule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualityManageLadderRule::getRuleDimType,dimType)
                .eq(QualityManageLadderRule::getRuleType,type)
                .eq(QualityManageLadderRule::getRuleOrderId,orderId)
                .eq(QualityManageLadderRule::getRuleOrgNo,orgNo);
        return getOne(wrapper);
    }

    @Override
    public List<QualityManageLadderRule> getByParam(QualityManageLadderRulePO po) {
        LambdaQueryWrapper wrapper = wrapper(po);
        return list(wrapper);
    }

    @Override
    public void saveOrUpdateBatch(List<QualityManageLadderRule> rules,Integer ruleDimType, Integer ruleType) {
        QualityManageLadderRulePO po = new QualityManageLadderRulePO();
        po.setRuleDimType(ruleDimType);
        po.setRuleType(ruleType);
        List<QualityManageLadderRule> oldList = getByParam(po);
        List<Long> delIds = new ArrayList<>();
        for(QualityManageLadderRule rule : rules){
            QualityManageLadderRule old = getByUnique(rule.getRuleDimType(),rule.getRuleType(),rule.getRuleOrderId(),rule.getRuleOrgNo());
            if(Objects.isNull(old)){
                save(rule);
            }else{
                rule.setRuleId(old.getRuleId());
                updateById(rule);
            }
        }

        //原记录多出需删除
        for(QualityManageLadderRule old : oldList){
            boolean result = rules.stream().anyMatch(
                    o -> o.getRuleDimType().equals(old.getRuleDimType())
                            && o.getRuleType().equals(old.getRuleType())
                            && o.getRuleOrderId().equals(old.getRuleOrderId())
                            && o.getRuleOrgNo().equals(old.getRuleOrgNo())
            );
            if(!result){
                delIds.add(old.getRuleId());
            }
        }
        removeByIds(delIds);
    }

    @Override
    public BigDecimal getRuleScore(QualityManageLadderRulePO po) {
        LambdaQueryWrapper wrapper = wrapper(po);
        QualityManageLadderRule rule = getOne(wrapper);
        if (Objects.nonNull(rule)){
            return rule.getRuleScore();
        }
        return null;
    }

    @Override
    public List<QualityManageLadderRule> getByDimType(Integer ruleDimType) {
        QualityManageLadderRulePO po = new QualityManageLadderRulePO();
        po.setRuleDimType(ruleDimType);
        LambdaQueryWrapper wrapper = wrapper(po);
        List<QualityManageLadderRule> list = list(wrapper);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list;
    }

    public LambdaQueryWrapper wrapper(QualityManageLadderRulePO po){
        LambdaQueryWrapper<QualityManageLadderRule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(po.getRuleId()),QualityManageLadderRule::getRuleId,po.getRuleId())
                .eq(Objects.nonNull(po.getRuleDimType()),QualityManageLadderRule::getRuleDimType,po.getRuleDimType())
                .eq(Objects.nonNull(po.getRuleType()),QualityManageLadderRule::getRuleType,po.getRuleType())
                .eq(Objects.nonNull(po.getRuleOrgNo()),QualityManageLadderRule::getRuleOrgNo,po.getRuleOrgNo())
                .eq(Objects.nonNull(po.getRuleAgentNo()),QualityManageLadderRule::getRuleAgentNo,po.getRuleAgentNo())
                .ge(Objects.nonNull(po.getRate()),QualityManageLadderRule::getRuleLowerBound,po.getRate())
                .lt(Objects.nonNull(po.getRate()),QualityManageLadderRule::getRuleUpperBound,po.getRate());
        return wrapper;
    }
}

